package com.ca.ridersclubapplication.view.bar;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.ca.ridersclubapplication.R;
import com.ca.ridersclubapplication.util.ImageUtils;

public class AccountTitleBar extends ConstraintLayout {

    private String headerUrl;
    private String name;

    public ImageView backView;
    public ImageView headerView;
    public Button followView;
    public TextView nameView;
    public ImageView chatView;

    public AccountTitleBar(@NonNull Context context) {
        super(context);
        initView(context, null);
    }

    public AccountTitleBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public AccountTitleBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {

        View view = LayoutInflater.from(context).inflate(R.layout.account_title_bar_layout, this);

        backView = view.findViewById(R.id.iv_back);
        headerView = view.findViewById(R.id.iv_header);
        nameView = view.findViewById(R.id.tv_name);
        followView = view.findViewById(R.id.iv_follow);
        chatView = view.findViewById(R.id.iv_chat);

        if (attrs != null) {
            TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MiniMainTitleBar, 0, 0);

            headerUrl = array.getString(R.styleable.MiniMainTitleBar_mmtb_headerUrl);
            name = array.getString(R.styleable.MiniMainTitleBar_mmtb_name);

            array.recycle();
        }

        setHeaderUrl(headerUrl);
        setName(name);
    }

    public String getHeaderUrl() {
        return headerUrl;
    }

    public void setHeaderUrl(String headerUrl) {
        this.headerUrl = headerUrl == null ? "" : headerUrl;
        ImageUtils.loadCircleImage(getContext(), this.headerUrl, headerView);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? "" : name;
        this.nameView.setText(this.name);
    }
}
